package cn.mastor.org.controller;

import cn.mastor.basic.anno.PreAuthorize;
import cn.mastor.basic.util.EncryptionUtil;
import cn.mastor.basic.util.PageList;
import cn.mastor.basic.util.StrUtils;
import cn.mastor.org.domain.Employee;
import cn.mastor.org.query.EmployyQuery;
import cn.mastor.org.service.IEmployyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/emp")
@RestController
public class EmployyController {
    @Autowired
    private IEmployyService service;
    @GetMapping()
    public List findAll() {
        System.out.println(service.findAll());
        return  service.findAll();
    }
    @GetMapping("/list/{id}")
    public List findByShopId(@PathVariable("id") Long id) {
//        System.out.println(service.findAll());
        return  service.findByShopId(id);
    }
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id) {
        return service.findById(id);
    }
    @PostMapping
    public PageList<Employee> findbyPage(@RequestBody EmployyQuery query){
        return service.queryByPage(query);
    }
    @PutMapping
    public void addOrUpdate(@RequestBody Employee employee){
        if (null == employee.getId()){
            // 添加
            String randomString = StrUtils.getRandomString(32);
            employee.setSalt(randomString);
            String npwd = EncryptionUtil.encryption(employee.getPassword(), randomString);
            employee.setPassword(npwd);
            service.add(employee);
        }else {
            // 修改
            String randomString = StrUtils.getRandomString(32);
            employee.setSalt(randomString);
            String npwd = EncryptionUtil.encryption(employee.getPassword(), randomString);
            employee.setPassword(npwd);
            service.update(employee);
        }
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Long id){
        service.delete(id);
    }
    @PatchMapping()
    public void patchdel(@RequestBody List<Long> ids) {
        service.batchDel(ids);
    }

}
